package com.zabra.zl.ui.godeal.presenter;

import com.zabra.zl.App;
import com.zabra.zl.base.BasePresenter;
import com.zabra.zl.data.DetailsDealBean;
import com.zabra.zl.ui.godeal.contract.GoDealContract;
import com.zabra.zl.ui.godeal.model.GoDealModel;
import com.zabra.zl.utils.SharedPreferencesUtil;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GoDealPresenter extends BasePresenter<GoDealContract.View> implements GoDealContract.Presenter {
    private GoDealContract.Model model;

    public GoDealPresenter() {
        model=new GoDealModel();
    }

    @Override
    public void getData(RequestBody body) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getData(body).enqueue(new Callback<DetailsDealBean>() {
            @Override
            public void onResponse(Call<DetailsDealBean> call, Response<DetailsDealBean> response) {
                mView.hideLoading();
                mView.onSuccess(response.body());
                SharedPreferencesUtil.getInstance(App.context).putSP("token",response.headers().get("token"));
            }

            @Override
            public void onFailure(Call<DetailsDealBean> call, Throwable t) {
                mView.hideLoading();
                mView.onError(t);
            }
        });
    }
}
